using System.Buffers;
using SuperSocket.ProtoBase;

namespace KestrelClientWithSuperSocket;

internal sealed class EachPipeLineFilter : FixedHeaderPipelineFilter<EachPackage>
{
    private const int HeadSize = sizeof(short);

    public EachPipeLineFilter()
        : base(HeadSize)
    {
    }

    protected override EachPackage DecodePackage(ref ReadOnlySequence<byte> buffer)
    {
        var reader = new SequenceReader<byte>(buffer.Slice(HeadSize));

        var content = reader.ReadString();
        
        return new EachPackage
        {
            Content = content,
        };
    }

    protected override int GetBodyLengthFromHeader(ref ReadOnlySequence<byte> buffer)
    {
        var reader = new SequenceReader<byte>(buffer);

        reader.TryReadBigEndian(out short bodyLength);

        return bodyLength;
    }
}